
CC=gcc
AR=ar

#
# Modify these params if the commands are somewhere else
#

SED=sed
CP=cp
MV=mv
LN=ln -sf
RM=rm -f
CHMOD=chmod

AFC_CFLAGS=`afc-config --cflags`
AFC_LIBS=`afc-config --libs`

# the following are used only to build the "pyconnector.test" and "pyconnector.o" targets
# ----
PYTHON_STATIC=`echo "from distutils import sysconfig; print '%s/%s' % ( sysconfig.get_config_var ( 'LIBPL' ), sysconfig.get_config_var ( 'LDLIBRARY' ) )" | python`

# Needed by libpython
OTHER_LIBS=`echo "from distutils import sysconfig; print '%s %s' % ( sysconfig.get_config_var ( 'LIBS' ), sysconfig.get_config_var ( 'MODLIBS' ) )" | python` -lm

PYTHON_INCLUDE_PATH=`echo "from distutils import sysconfig; print sysconfig.get_python_inc()" | python`

PYTHON_LIBS=$(PYTHON_STATIC) $(OTHER_LIBS)
# ----

CFLAGS=-fPIC -g -Wall -O2 $(AFC_CFLAGS)
LIBS=$(PYTHON_LIBS) $(AFC_LIBS)
LIBFLAGS=-shared 

#
# Modify these params to copy files in different locations
#
LIBDIR=/usr/local/lib
BINDIR=/usr/local/bin
INCDIR=/usr/local/include

OBJS=../base.o ../string.o ../array_master.o ../dynamic_class_master.o ../dynamic_class.o ../hash_master.o ../dictionary.o ../nodemaster.o ../stringnode.o ../mem_tracker.o

dummy: pyconnector.so

test:	pyconnector.test

pyconnector.test:	pyconnector.c pyconnector.h  $(OBJS)
	@$(CC) -DTEST_CLASS $(CFLAGS) -o pyconnector.test -g -Wall pyconnector.c -I$(PYTHON_INCLUDE_PATH) $(OBJS) $(PYTHON_LIBS)

pyconnector.o:	pyconnector.c pyconnector.h
	@echo "Building: $@"
	@$(CC) -c $(CFLAGS) $< -o $@ -I$(PYTHON_INCLUDE_PATH)

pyconnector.so: pyconnector.c pyconnector.h
	python setup.py build

install: pyconnector.so
	@echo "Installing"

	@echo "   ... Python extension module"
	python setup.py install

	@echo "   ... shared library"
	@$(CP) build/lib/afcpyconnector.so $(LIBDIR)/libafcpyconnector.so.1

	@echo "   ... creating symlinks"
	@$(LN) $(LIBDIR)/libafcpyconnector.so.1 $(LIBDIR)/libafcpyconnector.so

	@echo "Copying header files"
	@if [ -d $(INCDIR)/afc ]; then echo "AFC Directory already exists: just copying files..."; else mkdir $(INCDIR)/afc; fi 
	@cp *.h $(INCDIR)/afc
	ldconfig
	@echo "   ... DONE!"


clean:
	@$(RM) -f ./*.o
	@$(RM) -f pyconnector.test 2>/dev/null
	@$(RM) -rf build
	@$(RM) -f ./*.so
